package ${pojoPackage};

import java.util.*;
import java.text.SimpleDateFormat;
import ${extPackage}.${className_d}Ext;
@SuppressWarnings("unchecked")
/**
 * ${className}
 */
public class ${className_d} {
	<#list tableCarrays as tableCarray>
	/**
	 * ${tableCarray.carrayName}
	 */
	public ${tableCarray.carrayType} ${tableCarray.carrayName_x};
	</#list>
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static ${className_d}Ext DB = new ${className_d}Ext(); 
	
	public ${className_d}() {
		super();
	}
	
	public ${className_d}(${stringCarrayNames2}) {
		super();
		<#list tableCarrays as tableCarray>
		this.${tableCarray.carrayName_x} = ${tableCarray.carrayName_x}; 
		</#list>
	}
	
	<#list tableCarrays as tableCarray>
	public ${tableCarray.carrayType} get${tableCarray.carrayName_d}() {
		return ${tableCarray.carrayName_x};
	}

	public void set${tableCarray.carrayName_d}(${tableCarray.carrayType} ${tableCarray.carrayName_x}) {
		this.${tableCarray.carrayName_x} = ${tableCarray.carrayName_x};
	} 
	
	<#if tableCarray.carrayType=="short" || tableCarray.carrayType=="int" || tableCarray.carrayType=="long" || tableCarray.carrayType=="float" || tableCarray.carrayType=="double">
	public void change${tableCarray.carrayName_d}With(${tableCarray.carrayType} ${tableCarray.carrayName_x}){
		this.${tableCarray.carrayName_x} += ${tableCarray.carrayName_x};
	}
	
	public void change${tableCarray.carrayName_d}WithMin(${tableCarray.carrayType} ${tableCarray.carrayName_x},${tableCarray.carrayType} min){
		this.${tableCarray.carrayName_x} += ${tableCarray.carrayName_x};
		this.${tableCarray.carrayName_x} = this.${tableCarray.carrayName_x} < min ? min : this.${tableCarray.carrayName_x};
	}
	
	public void change${tableCarray.carrayName_d}WithMax(${tableCarray.carrayType} ${tableCarray.carrayName_x},${tableCarray.carrayType} max){
		this.${tableCarray.carrayName_x} += ${tableCarray.carrayName_x};
		this.${tableCarray.carrayName_x} = this.${tableCarray.carrayName_x} > max ? max : this.${tableCarray.carrayName_x};
	}
	
	public void change${tableCarray.carrayName_d}WithMaxMin(${tableCarray.carrayType} ${tableCarray.carrayName_x},${tableCarray.carrayType} max,${tableCarray.carrayType} min){
		this.${tableCarray.carrayName_x} += ${tableCarray.carrayName_x};
		this.${tableCarray.carrayName_x} = this.${tableCarray.carrayName_x} < min ? min : this.${tableCarray.carrayName_x};
		this.${tableCarray.carrayName_x} = this.${tableCarray.carrayName_x} > max ? max : this.${tableCarray.carrayName_x};
	}	
	</#if>
	</#list>
	
	public Map ${className_x}ToMap() {
		Map map = new HashMap();
		try {
			<#list tableCarrays as tableCarray>
			<#assign typeBeing=""/>
			<#assign typeEnd=""/>
			<#if tableCarray.carrayType=="java.util.Date">
				<#assign typeBeing="simpleDateFormat.format("/>
				<#assign typeEnd=")"/>
			</#if>
			map.put("${tableCarray.carrayName_x}",${typeBeing}this.${tableCarray.carrayName_x}${typeEnd});
			</#list>
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	public ${className_d} mapTo${className_d}(Map map){
		try {
			<#list tableCarrays as tableCarray>
			<#assign typeBeing=""/>
			<#assign typeEnd=""/>
			<#if tableCarray.carrayType=="int">
				<#assign typeBeing="Integer.parseInt("/>
				<#assign typeEnd=")"/>
			</#if>
			<#if tableCarray.carrayType=="double">
				<#assign typeBeing="Double.parseDouble("/>
				<#assign typeEnd=")"/>
			</#if>
			<#if tableCarray.carrayType=="boolean">
				<#assign typeBeing="Boolean.parseBoolean("/>
				<#assign typeEnd=")"/>
			</#if>
			<#if tableCarray.carrayType=="long">
				<#assign typeBeing="Long.parseLong("/>
				<#assign typeEnd=")"/>
			</#if>
			<#if tableCarray.carrayType=="java.util.Date">
				<#assign typeBeing="simpleDateFormat.parse("/>
				<#assign typeEnd=")"/>
			</#if>
			${tableCarray.carrayType} ${tableCarray.carrayName_x} = ${typeBeing} map.get("${tableCarray.carrayName_x}").toString()${typeEnd};
			</#list>
			${className_d} ${className_x} = new ${className_d}(${stringCarrayNames1});
			return ${className_x};
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	} 
	
	public String toString(){
		return ${className_x}ToMap().toString();
	}
	
	//方便数据操作	
	public ${className_d} insert(){
		return DB.insert(this);
	}
	<#list tableIndexs as tableIndex>
	<#if tableIndex.indexName=="PRIMARY">	
	public ${className_d} update(){
		return DB.update(this);
	}
	
	public int delete(){
		return DB.delete(this);
	}
	
	</#if>		
	</#list>
	<#list tableBinds as tableBind>
	<#if tableBind.keyType=="exportedKey">
	
	public List<${tableBind.tableName_d}> getFK_${tableBind.tableName_d}s_${tableBind.carrayName_d}(){
		return DB.getFK_${tableBind.tableName_d}s_${tableBind.carrayName_d}(this);
	}
	
	</#if>
	<#if tableBind.keyType=="importedKey">
	
	public ${tableBind.tableName_d} getFk_${tableBind.tableName_d}_${tableBind.carrayName_d}(){
		return DB.getFk_${tableBind.tableName_d}_${tableBind.carrayName_d}(this);
	}
	
	</#if>
	</#list>	
}
